package com.lngsyz.eseal.base.biz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lngsyz.eseal.base.biz.service.BizService;
import com.lngsyz.eseal.base.entity.CommonConstant;
import com.lngsyz.eseal.base.entity.LoginInfo;
import com.lngsyz.eseal.base.entity.URIResult;
import com.lngsyz.eseal.base.entity.biz.AppInfoForAdmin;
import com.lngsyz.eseal.base.entity.biz.AppInfoForNormal;
import com.lngsyz.eseal.base.entity.biz.GetCurrentInfo;
import com.lngsyz.eseal.base.entity.biz.InAddApp;
import com.lngsyz.eseal.base.entity.biz.InAddAppFile;
import com.lngsyz.eseal.base.entity.biz.InApplyReview;
import com.lngsyz.eseal.base.entity.biz.InGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.InGetTicket;
import com.lngsyz.eseal.base.entity.biz.InOpen;
import com.lngsyz.eseal.base.entity.biz.InReviewAppInfo;
import com.lngsyz.eseal.base.entity.biz.InUserInfo;
import com.lngsyz.eseal.base.entity.biz.OutGetAppFile;
import com.lngsyz.eseal.base.entity.biz.OutGetRefuseReason;
import com.lngsyz.eseal.base.entity.biz.OutGetTicket;
import com.lngsyz.eseal.base.entity.biz.OutUserInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@Api(tags = "BizController", description = "业务接口")
@RequestMapping("v1/biz")
public class BizController {

	@Autowired()
	private BizService bizService;
	
	@ApiOperation(value = "获取当前信息", notes = "获取当前信息")
	@RequestMapping(value = "/getCurrentInfo", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<GetCurrentInfo> getCurrentInfo(
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<GetCurrentInfo> res = new URIResult<GetCurrentInfo>();
		try {
			GetCurrentInfo data = bizService.getCurrentInfo(loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取授权密钥", notes = "获取授权密钥")
	@RequestMapping(value = "/getTicket", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<OutGetTicket> getTicket(
			@ApiParam(name = "id", value = "对接应用的ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<OutGetTicket> res = new URIResult<OutGetTicket>();
		try {
			InGetTicket inGetTicket = new InGetTicket();
			inGetTicket.setId(id);
			OutGetTicket data = bizService.getTicket(inGetTicket, loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "【普通用户】提交申请", notes = "【普通用户】提交申请")
	@RequestMapping(value = "/addApp", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addApp(InAddApp inAddApp,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.addApp(inAddApp, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "获取全市刻章单位列表", notes = "获取全市刻章单位列表")
	@RequestMapping(value = "/getAppInfoForNormal", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<AppInfoForNormal>> getAppInfoForNormal(
			@ApiParam(name = "page", value = "分页页码", required = true) @RequestParam(value = "page", defaultValue = "0") int page,
			@ApiParam(name = "size", value = "每页显示数量", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<AppInfoForNormal>> res = new URIResult<List<AppInfoForNormal>>();
		try {
			List<AppInfoForNormal> data = bizService.getAppInfoForNormal(loginInfo, page, size);
			int total = bizService.getAppCountForNormal(loginInfo);
			res.setData(data);
			res.setTotal(total);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "获取提交关联的文件", notes = "获取提交关联的文件")
	@RequestMapping(value = "/getAppFile", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<OutGetAppFile> getAppFile(
			@ApiParam(name = "id", value = "申请对接应用的主键", required = false) @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<OutGetAppFile> res = new URIResult<OutGetAppFile>();
		try {
			OutGetAppFile data = bizService.getAppFile(id, loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "【普通用户】补全材料（上传文件资质）", notes = "【普通用户】补全材料（上传文件资质）")
	@RequestMapping(value = "/addAppFile", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> addAppFile(@RequestParam("id") String id,
			@RequestParam("file1") MultipartFile file1,
			@RequestParam("file2") MultipartFile file2,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			InAddAppFile inAddAppFile = new InAddAppFile();
			inAddAppFile.setId(id);
			inAddAppFile.setFile1(file1);
			inAddAppFile.setFile2(file2);
			bizService.addAppFile(inAddAppFile, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}

	@ApiOperation(value = "【管理员】待审核列表", notes = "【管理员】待审核列表")
	@RequestMapping(value = "/getAppInfoForAdmin", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<AppInfoForAdmin>> getAppInfoForAdmin(
			@ApiParam(name = "account", value = "申请人手机号", required = false) @RequestParam(value = "account", defaultValue = "") String account,
			@ApiParam(name = "sysName", value = "系统名称", required = false) @RequestParam(value = "sysName", defaultValue = "") String sysName,
			@ApiParam(name = "applyName", value = "申请人姓名", required = false) @RequestParam(value = "applyName", defaultValue = "") String applyName,
			@ApiParam(name = "applyDept", value = "申请人所属部门", required = false) @RequestParam(value = "applyDept", defaultValue = "") String applyDept,
			@ApiParam(name = "applyPassFlag", value = "【1】基本信息审核通过（0：待审核 1：审核通过 2：审核未通过）", required = false) @RequestParam(value = "applyPassFlag", defaultValue = "") String applyPassFlag,
			@ApiParam(name = "openFlag", value = "【3】启用标记（1：开启 2：关闭）", required = false) @RequestParam(value = "openFlag", defaultValue = "") String openFlag,
			@ApiParam(name = "page", value = "分页页码", required = true) @RequestParam(value = "page", defaultValue = "0") int page,
			@ApiParam(name = "size", value = "每页显示数量", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<AppInfoForAdmin>> res = new URIResult<List<AppInfoForAdmin>>();
		try {
			InReviewAppInfo inReviewAppInfo = new InReviewAppInfo();
			inReviewAppInfo.setAccount(account);
			inReviewAppInfo.setSysName(sysName);
			inReviewAppInfo.setApplyName(applyName);
			inReviewAppInfo.setApplyDept(applyDept);
			inReviewAppInfo.setApplyPassFlag(applyPassFlag);
			inReviewAppInfo.setOpenFlag(openFlag);
			List<AppInfoForAdmin> data = bizService.getAppInfoForAdmin(inReviewAppInfo, loginInfo, page, size);
			int total = bizService.getAppCountForAdmin(inReviewAppInfo, loginInfo);
			res.setData(data);
			res.setTotal(total);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "【管理员】用户列表", notes = "【管理员】用户列表")
	@RequestMapping(value = "/getUserInfo", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<List<OutUserInfo>> getUserInfo(
			@ApiParam(name = "account", value = "账号", required = false) @RequestParam(value = "account", defaultValue = "") String account,
			@ApiParam(name = "district", value = "区划", required = false) @RequestParam(value = "district", defaultValue = "") String district,
			@ApiParam(name = "admin", value = "管理员（0：普通 1：管理员）", required = false) @RequestParam(value = "admin", defaultValue = "") String admin,
			@ApiParam(name = "page", value = "分页页码", required = true) @RequestParam(value = "page", defaultValue = "0") int page,
			@ApiParam(name = "size", value = "每页显示数量", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<List<OutUserInfo>> res = new URIResult<List<OutUserInfo>>();
		try {
			InUserInfo inUserInfo = new InUserInfo();
			inUserInfo.setAccount(account);
			inUserInfo.setDistrict(district);
			inUserInfo.setAdmin(admin);
			List<OutUserInfo> data = bizService.getUserInfo(inUserInfo, loginInfo, page, size);
			int total = bizService.getUserInfoCount(inUserInfo, loginInfo);
			res.setData(data);
			res.setTotal(total);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "【管理员】审核通过/不通过", notes = "【管理员】审核通过/不通过")
	@ApiImplicitParam(name = "inApplyReview", value = "【管理员】审核通过/不通过", dataType = "InApplyReview", required = true, paramType = "body")
	@RequestMapping(value = "/reviewApp", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> reviewApp(@RequestBody InApplyReview inApplyReview,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.reviewApp(inApplyReview, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "【管理员】启用/停用", notes = "【管理员】启用/停用")
	@ApiImplicitParam(name = "inOpen", value = "【管理员】启用/停用", dataType = "InOpen", required = true, paramType = "body")
	@RequestMapping(value = "/open", method = RequestMethod.POST)
	@ResponseBody
	public URIResult<String> open(@RequestBody InOpen inOpen,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.open(inOpen, loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "【普通用户】查看被驳回原因", notes = "【普通用户】查看被驳回原因")
	@RequestMapping(value = "/getRefuseReason", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<OutGetRefuseReason> getRefuseReason(
			@ApiParam(name = "id", value = "对接应用的ID") @RequestParam(value = "id", defaultValue = "") String id,
			@ApiIgnore LoginInfo loginInfo) {
		URIResult<OutGetRefuseReason> res = new URIResult<OutGetRefuseReason>();
		try {
			InGetRefuseReason inGetRefuseReason = new InGetRefuseReason();
			inGetRefuseReason.setId(id);
			OutGetRefuseReason data = bizService.getRefuseReason(inGetRefuseReason, loginInfo);
			res.setData(data);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "退出", notes = "退出")
	@RequestMapping(value = "/logout", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> logout(@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.logout(loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
	
	@ApiOperation(value = "刷新token", notes = "刷新token，长时间不操作，刷新token会报异常，页面10分钟自动刷新token")
	@RequestMapping(value = "/refreshToken", method = { RequestMethod.GET })
	@ResponseBody
	public URIResult<String> refreshToken(@ApiIgnore LoginInfo loginInfo) {
		URIResult<String> res = new URIResult<String>();
		try {
			bizService.refreshToken(loginInfo);
			res.setCode(CommonConstant.CODE_SUCCESS);
		} catch (Exception e) {
			res.setCode(CommonConstant.CODE_FAILURE);
			res.setInfo(e.getMessage());
		}
		return res;
	}
}